//获取应用实例
import {
  Api
} from '../../config/api.js'
import {
  request, WXShowModal, WXShowToast
} from '../../utils/index.js'
Page({
  data: {
    signedDay: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    let that = this

    function isid() {
      var token = wx.getStorageSync('token')
      var {
        id
      } = wx.getStorageSync('userInfo')
      console.log(!isNaN(id))
      if (!isNaN(id) && token) {
        that.setData({
          id
        })
      } else {
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }
    };
    isid() //检查是否登录

    //   获取签到详情
    async function _getsign() {
      let {
        data
      } = await request({
        url: Api.sign,
        data: {},
        method: 'GET'
      })


       let dz = ()=>{
         let t = []
        getDates().forEach((item,index)=>{
          t.push({date:item.split('-')[2]}) 
        })
        return t
       }
      //  data.data.signData.forEach((item,index)=>{
      //    console.log(item)
      //  })
     let ds = dz()
      dz().forEach((item,index)=>{
        if(index ==0){ds[index].xq = '一'}
        if(index ==1){ds[index].xq = '二'}
        if(index ==2){ds[index].xq = '三'}
        if(index ==3){ds[index].xq = '四'}
        if(index ==4){ds[index].xq = '五'}
        if(index ==5){ds[index].xq = '六'}
        if(index ==6){ds[index].xq = '日'}
         console.log(data)
         try {
          if(data.data.signData != []||data.data!= null ){
            data.data.signData.forEach((i,x)=>{
              // console.log(i.sign_time.split('-')[2].split(' ')[0])
                if(item.date == i.sign_time.split('-')[2].split(' ')[0]){
                    ds[index].sign_time=i.sign_time
    
                   
                  // console.log(i.sign_time.split('-')[2].split(' ')[0])
                }else{
                  // console.log('false')
                }
             })
          }
         } catch (error) {
           
         }
       })
      //  console.log(ds)
       if(ds){
        that.setData({
          ds,
        })
       }
      try {
        if(data.data.sheep_coin.split('.')[0]){
          that.setData({
             'sheep_coin':data.data.sheep_coin.split('.')[0]
          })
         }
      } catch (error) {
        
      }
      try {
        if(data.data){
          that.setData({
            'data': data.data,
          })
         }
      } catch (error) {
        
      }
       

       
    }


    _getsign()
     //   获取羊币
    //  async function _sheep_coin() {
    //   let {
    //     data
    //   } = await request({
    //     url: Api.sheep_coin,
    //     data: {},
    //     method: 'POST'
    //   })

    //   // console.log(data.data)
    //   that.setData({
    //     'sheep_coin': data.data.sleep_coin.split('.')[0]
    //   })
    // }

    // _sheep_coin()

    async function _getrq(){
      let date = new Date()
      that.setData({
        year:date.getFullYear(),
        month:date.getMonth() + 1
      })
    }

    _getrq()
    
    function getDates(currentTime) {//JS获取当前周从星期一到星期天的日期

      var currentDate = new Date()
      var timesStamp = currentDate.getTime();
      var currenDay = currentDate.getDay();
      var dates = [];
      for (var i = 0; i < 7; i++) {
          dates.push(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).toLocaleDateString().replace(/\//g, '-'));
      }
      return dates
  }
  // console.log()
  },
  qd(){
    let that = this
    async function _sheep_coin() {
      let {
        data
      } = await request({
        url: Api.sign,
        data: {},
        method: 'POST'
      })
      if(data.code == '2001'){
        WXShowToast('签到成功')
        that.onShow()
      }
      if(data.code == '1001'){
        console.log(data)
        WXShowToast(data.msg)
      }
       console.log(data)
      // that.setData({
      //   'sheep_coin': data.data.sleep_coin.split('.')[0]
      // })
    }
    _sheep_coin()
  }
});